﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLHD.Utilities;
using QLHD.Models;

namespace STC.Thietbi.Web.Account
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ChangePasswordPushButton_Click(object sender, EventArgs e)
        {
            var curctrl = (TextBox)ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("CurrentPassword");
            var newctrl = (TextBox)ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("ConfirmNewPassword");
            var cfctrl = (TextBox)ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("NewPassword");

            string oldpass = curctrl.Text;
            string newpass = newctrl.Text;
            string cfnewpass = cfctrl.Text;

            string username = HttpContext.Current.User.Identity.Name;

            AuthenticationHelper authen = new AuthenticationHelper();
            if (authen.Authenticate(username, oldpass))
            {
                QLHD2013Entities db = new QLHD2013Entities();
                var us = db.Users.Where(q => q.UserName.ToLower() == username).FirstOrDefault();
                string pass = Utilities.Encrypt(newpass);
                us.PassWord = pass;
                db.SaveChanges();
                Response.Redirect(ChangeUserPassword.SuccessPageUrl);
            }
            else
            {
                var error = (Literal)ChangeUserPassword.ChangePasswordTemplateContainer.FindControl("FailureText");
                error.Text = "Mật khẩu cũ không đúng.";
            }
            
        }
    }
}
